#!/bin/ash -efu
#
# mki-copy-isolinux
#
# This file is part of mkimage
# Copyright (C) 2007-2009  Alexey Gladkov <gladkov.alexey@gmail.com>
#
# This file is covered by the GNU General Public License,
# which should be included with mkimage as the file COPYING.
#

. "${0%/*}"/mki-sh-functions

verbose "has started executing."

pkgs="${PACKAGES_REQUIRED_COPY_ISOLINUX:-}"

imgsubdir="${MKI_IMAGESUBDIR:-}"
imgsubdir="${imgsubdir#/}"

[ -d "$chroot" ] ||
	fatal "$dir: not look like a work directory of hasher."

mki-install $pkgs ||
	fatal "failed to install packages: $pkgs."

[ -f "$chroot/.image/$imgsubdir/syslinux/isolinux.cfg" ] ||
	message "Warning: /.image/$imgsubdir/syslinux/isolinux.cfg: not found."

run_chrooted "$chroot" <<EOF
#!/bin/sh -efu

if [ -L /boot/vmlinuz ]; then
	kimage="\$(readlink -ev /boot/vmlinuz)"
else
	kimage="\$(find /boot -type f -name 'vmlinuz-*' -print -quit)"
fi

mkdir $verbose -p -- /.image/$imgsubdir/syslinux
cd /.image/$imgsubdir/syslinux

if [ -n "\$kimage" ]; then
	mkdir $verbose alt0
	cd /.image/$imgsubdir/syslinux

	cp $verbose -af \$kimage alt0/vmlinuz
	[ ! -f /boot/full.cz ] ||
		cp $verbose -af /boot/full.cz alt0/full.cz
fi

cp $verbose -f /usr/lib/syslinux/isolinux.bin .

[ ! -f /usr/lib/syslinux/gfxboot.c32 ] ||
	cp $verbose -af /usr/lib/syslinux/gfxboot.c32 .

[ ! -f /usr/lib/syslinux/gfxboot.com ] ||
	cp $verbose -af /usr/lib/syslinux/gfxboot.com .

isolinux-config --base /syslinux ./isolinux.bin
EOF
